package com.scanner;

import android.graphics.Point;

public class FrameContour {

	int ANGLE_THRESH = 5;
	double MOVE_RATIO_THRESH = 0.2;
	int distanceError = 10;
	int errors = 0;
	
	public Point topLeft;
	public Point topRight;
	public Point bottomLeft;
	public Point bottomRight;
	
	private boolean started = false;
	
	public FrameContour()
	{
		topLeft = new Point();
		topRight = new Point();
		bottomLeft = new Point();
		bottomRight = new Point();
	}
	
	public FrameContour(Point tL, Point tR, Point bR, Point bL)
	{
		UpdatePoints(tL,tR,bR,bL);
	}
	
	private boolean DistantPoints(Point p1, Point p2)
	{
		if (Math.sqrt( Math.pow(Math.abs(p1.x - p2.x), 2) + Math.pow(Math.abs(p1.y - p2.y), 2)) > distanceError)
		{
				return true;
		}
		return false;
	}
	
	private boolean NewPointsAreFar(Point tL, Point tR, Point bR, Point bL)
	{
		if (DistantPoints(tL, topLeft) || DistantPoints(tR, topRight) ||
				DistantPoints(bR, bottomRight) || DistantPoints(bL, bottomLeft))
		{
			return true;
		}
		return false;
	}
	
	public void UpdatePoints(Point tL, Point tR, Point bR, Point bL)
	{
		started = true;
		
		if (NewPointsAreFar(tL,tR,bR,bL) && errors < 2)
		{
			errors += 1;
			return;
		}
		errors = 0;
		topLeft = tL;
		topRight = tR;
		bottomRight = bR;
		bottomLeft = bL;
	}
	
	public MoveActions TurnState(){
		float angle = (float) Math.toDegrees(Math.atan2(topRight.x- topLeft.x, topRight.y - topLeft.y));
		if(angle < 0){
	        angle += 360;
	    }
	    angle = angle -90;
	    if (angle > ANGLE_THRESH){
	    	return MoveActions.TURN_ANTICLOCK;
	    }
	    if (angle < -ANGLE_THRESH){
		    return MoveActions.TURN_CLOCK;
		}
	    return MoveActions.DONT_TURN;
	}
	
	public boolean Started()
	{
		return started;
	}
	
	public MoveActions MoveState(double height, double width){
		double Left = topLeft.x;
		double Right = width - topRight.x;
		double horizontalRatio = Left/Right;
		
		double UP = topLeft.y;
		double Down = height - bottomLeft.y;
		double verticalRatio = UP/Down;
		
		if (Math.abs(1-verticalRatio) > Math.abs(1-horizontalRatio))
		{
		    if(verticalRatio <  1 - MOVE_RATIO_THRESH){
		    	return MoveActions.MOVE_DOWN;
		    }
		    
		    if(verticalRatio > 1 + MOVE_RATIO_THRESH){
		    	return MoveActions.MOVE_UP;
		    }
		    
		    return MoveActions.DONT_MOVE;
		}
		else
		{
			if(horizontalRatio < 1 - MOVE_RATIO_THRESH){
		    	return MoveActions.MOVE_RIGHT;
		    }
		    
		    if(horizontalRatio > 1 + MOVE_RATIO_THRESH){
		    	return MoveActions.MOVE_LEFT;
		    }
		     
		    return MoveActions.DONT_MOVE;
		}
	}

	public MoveActions ZoomState(double width, double length){
		double RectArea = Math.sqrt(Math.pow((bottomLeft.x- topLeft.x),2)+ Math.pow(bottomLeft.y - topLeft.y,2))  
				*  Math.sqrt(Math.pow(topRight.x- topLeft.x,2)+ Math.pow(topRight.y - topLeft.y,2));
		double FrameArea = width * length;
		double Ratio = RectArea/FrameArea;
	    if(Ratio < 0.90){
	    	return MoveActions.ZOOM_IN;
	    }

	    return MoveActions.DONT_ZOOM;
	}
	
	
	public MoveActions TiltState(){
		double distanceLeftSegment = Math.sqrt(Math.pow(bottomLeft.x- topLeft.x,2)+ Math.pow(bottomLeft.y - topLeft.y,2));
		double distanceRightSegment = Math.sqrt(Math.pow(bottomRight.x- topRight.x,2)+ Math.pow(bottomRight.y - topRight.y,2));
		double verticalRatio = distanceLeftSegment/distanceRightSegment;
		
		double distanceUpSegment =  Math.sqrt(Math.pow(topRight.x- topLeft.x,2)+ Math.pow(topRight.y - topLeft.y,2));
		double distanceDownSegment =  Math.sqrt(Math.pow(bottomRight.x- bottomLeft.x,2)+ Math.pow(bottomRight.y - bottomLeft.y,2));
		double horizontalRatio = distanceUpSegment/distanceDownSegment;
		
		if (Math.abs(1-horizontalRatio) > Math.abs(1-verticalRatio))
		{
			
		    if(horizontalRatio > 1 + MOVE_RATIO_THRESH){
		    	return MoveActions.TILT_DOWN;
		    }
		    if (horizontalRatio < 1 - MOVE_RATIO_THRESH){
		    	return MoveActions.TILT_UP;
		    }
		    return MoveActions.DONT_TILT;
		}
	    else
	    {
	    	if(verticalRatio > 1 + MOVE_RATIO_THRESH){
		    	return MoveActions.TILT_RIGHT;
		    }
		    if (verticalRatio < 1 - MOVE_RATIO_THRESH){
		    	return MoveActions.TILT_LEFT;
		    }
		    return MoveActions.DONT_TILT;
	    }
	}
	
}
